package com.yb.xczx.cloud.auth.oauth2.handler.mybatis;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.security.jackson2.SecurityJackson2Modules;
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module;
import org.springframework.util.StringUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/21 15:17
 * FileName: XczxSetTypeHandler
 * Description:
 */
public abstract class XczxSetTypeHandler<T> implements TypeHandler<Set> {

    private  Class<T> tClass;


    public XczxSetTypeHandler(){
        Type superClass = getClass().getGenericSuperclass();
        if (!(superClass instanceof ParameterizedType)) {
            throw new IllegalArgumentException("无泛型类型信息");
        }
        tClass = (Class<T>) ((ParameterizedType) superClass).getActualTypeArguments()[0];



    }

    @Override
    public void setParameter(PreparedStatement ps, int i, Set parameter, JdbcType jdbcType) throws SQLException {
        Set strSet = (Set) parameter.stream().map(item -> itemToString((T) item)).collect(Collectors.toSet());
        String param = StringUtils.collectionToCommaDelimitedString(strSet);
        ps.setString(i,param);
    }

    @Override
    public Set getResult(ResultSet rs, String columnName) throws SQLException {
        Set<String> clientAuthenticationMethods = StringUtils.commaDelimitedListToSet(rs.getString(columnName));
        Set<T> resultSet = clientAuthenticationMethods.stream().map(authMethod -> getResult(authMethod)).collect(Collectors.toSet());
        return resultSet;
    }

    @Override
    public Set getResult(ResultSet rs, int columnIndex) throws SQLException {
        Set<String> clientAuthenticationMethods = StringUtils.commaDelimitedListToSet(rs.getString(columnIndex));
        Set<T> resultSet = clientAuthenticationMethods.stream().map(authMethod -> getResult(authMethod)).collect(Collectors.toSet());
        return resultSet;
    }

    @Override
    public Set getResult(CallableStatement cs, int columnIndex) throws SQLException {
        Set<String> clientAuthenticationMethods = StringUtils.commaDelimitedListToSet(cs.getString(columnIndex));
        Set<T> resultSet = clientAuthenticationMethods.stream().map(authMethod -> getResult(authMethod)).collect(Collectors.toSet());
        return resultSet;
    }

    protected abstract T getResult(String resultStr);

    protected abstract String itemToString(T t);
}
